using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Security.Permissions;

using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Common.Security;
using Microsoft.ConnectionEngine.Common;

namespace Microsoft.ConnectionEngine.Administration.Core.ServiceProvider.Business
{
    public class MessageTypeManager
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <returns></returns>
        public static MessageTypesList Search(HSDPrincipal currentPrincipal, MessageTypeSearchRequest messageTypeSearchRequest)
        {
            if (!SecurityManager.UserInRoles(currentPrincipal, new String[2] { RoleTypes.Administrator, RoleTypes.Provider }))
                throw new SecurityException();

            string searchRequestXml = AdapterHelper.ConstructObjectXml(messageTypeSearchRequest, typeof(MessageTypeSearchRequest));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, searchRequestXml);                
            
            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1043, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return (MessageTypesList)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(MessageTypesList));
            
        }

        /// <summary>
        /// Retrieves a MessageType by Id
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="messageTypeID"></param>
        /// <returns></returns>
        public static MessageType SelectItem(HSDPrincipal currentPrincipal, int messageTypeID)
        {            
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "MessageTypeID", messageTypeID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1056, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return (MessageType)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(MessageType));
        }

        public static MessageType Lock(HSDPrincipal currentPrincipal, int messageTypeID)
        {
            if (!SecurityManager.UserInRoles(currentPrincipal, new String[2] { RoleTypes.Administrator, RoleTypes.Provider }))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "MessageTypeID", messageTypeID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1046, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return (MessageType)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(MessageType));
        }

        public static bool Unlock(HSDPrincipal currentPrincipal, int messageTypeID)
        {
            if (!SecurityManager.UserInRoles(currentPrincipal, new String[2] { RoleTypes.Administrator, RoleTypes.Provider }))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "MessageTypeID", messageTypeID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1045, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);
        }

        public static bool UnlockAll(HSDPrincipal currentPrincipal)
        {
            if (!SecurityManager.UserInRoles(currentPrincipal, new String[2] { RoleTypes.Administrator, RoleTypes.Provider }))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "MessageTypeUnlockAll", "");

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1048, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);
        }

        public static int Insert(HSDPrincipal currentPrincipal, MessageType messageType)
        {
            if (!SecurityManager.UserInRoles(currentPrincipal, new String[2] { RoleTypes.Administrator, RoleTypes.Provider }))
                throw new SecurityException();

            string messageTypeXml = AdapterHelper.ConstructObjectXml(messageType, typeof(MessageType));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, messageTypeXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1049, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return ((MessageType)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(MessageType))).MessageTypeID;
        }

        public static bool Update(HSDPrincipal currentPrincipal, MessageType messageType)
        {
            if (!SecurityManager.UserInRoles(currentPrincipal, new String[2] { RoleTypes.Administrator, RoleTypes.Provider }))
                throw new SecurityException();

            string messageTypeXml = AdapterHelper.ConstructObjectXml(messageType, typeof(MessageType));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, messageTypeXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1051, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return AdapterHelper.GetBooleanFromResponse(result);
        }

        public static bool Delete(HSDPrincipal currentPrincipal, int messageTypeID)
        {
            if (!currentPrincipal.IsInRole(RoleTypes.Administrator))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "MessageTypeID", messageTypeID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1053, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);
        }
       
    }
}
